import Vue from 'vue'
import VueRouter from 'vue-router';
Vue.use(VueRouter)

/**
 * 事件跳转路由方式防止路由报错
 */
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

import { Notify } from 'vant'

const routes = [
  {
    path: '/',
    redirect: '/index',
    component: () => import('../components/Layout.vue'),
    children: [{
      path: '/index',
      name: 'Index',
      component: () => import('../views/Index.vue')
    },
    {
      path: '/talkingTag',
      name: 'TalkingTag',
      component: () => import('../views/TalkingTag.vue')
    },
    {
      path: '/scene',
      name: 'Scene',
      component: () => import('../views/Scene.vue')
    },
    {
      path: '/attention',
      name: 'Attention',
      component: () => import('../views/Attention.vue')
    },
    {
      path: '/tagDetail',
      name: 'TagDetail',
      component: () => import('../views/TagDetail.vue'),
      meta: {
        showNavArrow: true
      }
    },
    // GirlSay
    {
      path: '/girlSay',
      name: 'GirlSay',
      component: () => import('../views/GirlSay.vue')
    },
    ]
  },
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  let hasLogin = false; //通过缓存在本地的登录信息或者vuex来判断权限
  //首页可以进，购物车、我的页面，需要登录
  if (to.name !== 'login' && to.meta.auth) {
    if (!hasLogin) {
      next({ name: 'login' })
    } else {
      next();
    }
  } else if (to.name == 'login' && hasLogin) {
    //若已登录，则返回之前的页面，给出提示
    // console.log('你已登录,跳转到首页');
    Notify({ type: 'warning', message: '你已登录,退出登录后重试' });
    next({ name: from.name });
  } else next()
})
export default router
